import java.util.*;
import java.io.*;
public class Problem {
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt(); // 读取测试用例数量
        for (int i = 0; i < t; i++) {
            int xc = scanner.nextInt(); // 读取xc
            int yc = scanner.nextInt(); // 读取yc
            int k = scanner.nextInt(); // 读取k
            int a=k;
            System.out.println(xc + " " + yc);
            int sumX=xc;
            int sumY=yc;
            int xDirection = 1; // x方向的步进，1表示正方向，-1表示负方向
            int yDirection = 0; // y方向的步进
            int step = 1; // 步进距离
            for (int j = 1; j < k-1; j++) {
                int x = xc + xDirection * step;
                int y = yc + yDirection * step;
                System.out.println(x + " " + y);
                sumX+=x;
                sumY+=y;
                if (xDirection == 1) {
                    xDirection = 0;
                    if (yDirection == 0) {
                        yDirection = 1;
                    } else {
                        yDirection = 0;
                        step++;
                    }
                } else {
                    xDirection = 1;
                }
            }
            if(k>1){
                int x=xc*a-sumX;
                int y=yc*a-sumY;
                System.out.println(x+" "+y);
            }

        }
        scanner.close();
    }
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt(); // 读取测试用例数量
        for (int i = 0; i < t; i++) {
            int xc = scanner.nextInt(); // 读取xc
            int yc = scanner.nextInt(); // 读取yc
            int k = scanner.nextInt(); // 读取k
            int sumX=0;
            int sumY=0;
            if(k==1){
                System.out.println(xc + " " + yc);
            }
            int x = xc;
            int y = yc;
            boolean moveX = true; // 初始方向为x轴

            for (int j = 0; j < k-1; j++) {
                if (moveX) {
                    x += (j % 2 == 1) ? 1 : -1;
                } else {
                    // 移动y坐标
                    y += (j % 2 == 1) ? 1 : -1;
                }
                System.out.println(x + " " + y);
                sumX+=x;
                sumY+=y;
                // 切换移动方向
                moveX = !moveX;
            }
            if(k>1){
                x=xc*k-sumX;
                y=yc*k-sumY;
                System.out.println(x+" "+y);
            }
        }
        scanner.close();
    }
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(reader.readLine());
        while (t-- > 0) {
            String[] parts = reader.readLine().split(" ");
            int n = Integer.parseInt(parts[0]);
            int k = Integer.parseInt(parts[1]);
            int[] a = new int[n];
            String[] aParts = reader.readLine().split(" ");
            for (int i = 0; i < n; i++) {
                a[i] = Integer.parseInt(aParts[i]);
            }
            int[] b = new int[n];
            String[] bParts = reader.readLine().split(" ");
            for (int i = 0; i < n; i++) {
                b[i] = Integer.parseInt(bParts[i]);
            }

            // Sort the array a and the binary array b
            Integer[] sortedIndices = new Integer[n];
            for (int i = 0; i < n; i++) {
                sortedIndices[i] = i;
            }
            Arrays.sort(sortedIndices, (i1, i2) -> a[i1] - a[i2]);

            // Apply the operation to minimize the elements in a with b[i] == 1
            for (int i = 0; i < n && k > 0; i++) {
                if (b[sortedIndices[i]] == 1) {
                    int increase = Math.min(k, a[sortedIndices[i + 1]] - a[sortedIndices[i]] - 1);
                    a[sortedIndices[i]] += increase;
                    k -= increase;
                }
            }

            // Calculate the maximum score
            int maxScore = 0;
            for (int i = 0; i < n; i++) {
                int medianIndex = (n - 1) / 2;
                int median = (n % 2 == 1) ? a[medianIndex] : (a[medianIndex] + a[medianIndex + 1]) / 2;
                maxScore = Math.max(maxScore, a[i] + median);
            }

            System.out.println(maxScore);
        }
        reader.close();
    }
}